package com.android.bbczw.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;

/**
 * http请求管理器,用于发送http消息
 * 
 * @author yangxing
 * @version $Id: HttpClientMgr.java, v 0.1 2011-10-27 下午04:47:46 yangxing Exp $
 */
public class HttpClientMgr {

	private static HttpClientMgr instance;
	private static DefaultHttpClient httpClient;
	private HttpPost httpPost;
	private HttpEntity httpEntity;
	private HttpResponse httpResponse;
	private String response;
	private String cookie;
	
	public static HttpClientMgr getInstance() {
		if(null == instance) {
			instance = new HttpClientMgr();
		}
		
		if(null == httpClient) {
			BasicHttpParams params = new BasicHttpParams();
			HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
			httpClient = new DefaultHttpClient(params);
		}
		
		return instance;
	}
	
	private void setPost(String parameter) {
		if(null != httpPost) {
			httpPost = null;
		}
		httpPost = new HttpPost(parameter);
		if(null != cookie) {
			httpPost.addHeader("Cookie", cookie);
		}
	}
	
	private void setEntity(Map<String, String> paramsMap, String chartype) {
		try {
			if(null != httpEntity) {
				httpEntity = null;
			}
			final List dataList = new ArrayList();  
			for(Map.Entry<String,String> entry : paramsMap.entrySet()) {
				dataList.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			httpEntity = new UrlEncodedFormEntity(dataList, chartype);
			httpPost.setEntity(httpEntity);  
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void setCookie() {
		Header header = httpResponse.getFirstHeader("Set-Cookie");
		if (header != null) {
			if(null == cookie) {
				cookie = header.getValue();
			}
	    }
	}
	
	public String execute(String postParam, Map<String, String> paramsMap, String chartype) {		
		setPost(postParam);
		setEntity(paramsMap, chartype);
		try {
			httpResponse = httpClient.execute(httpPost);
			setCookie();
			response = EntityUtils.toString(httpResponse.getEntity());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		if(null == cookies) {
//			//第一次登陆的情况
//			//cookies = 
//		}
		
		return response;
	}
}
